package com.example.emergencyattempt01.activity.administrator_activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class Update_User_Activity extends AppCompatActivity {

    EditText nameField;
    EditText passwordField;
    EditText instituteField;
    EditText identityField;

    String name;
    String password;
    String institute;
    String identity;
    long uid;

    Button cancel;
    Button confirm;

    String toServer;

    Handler handlerForUpdate=new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case UPDATE_SUCCESS:
                    Toast.makeText(Update_User_Activity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case UPDATE_FAIL:
                    Toast.makeText(Update_User_Activity.this,"修改失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    final int UPDATE_SUCCESS=0x015;
    final int UPDATE_FAIL=0x016;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user_layout);
        // 初始化界面
        initPage();
        // 设置取消监听
        setCancel();
        // 设置确认监听
        setConfirm();
    }

    private void setConfirm() {
      confirm.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             // 收集数据
              collectMessage();
              // 打包数据
              packMessage();
              // 联网，发送数据

                  new Thread(){
                      @Override
                      public void run() {
                          try {
                          ServerMessage serverMessage=NetWorkUtils.getInstance().create(toServer,"/update_user");
                          int code=serverMessage.getRespondCode();
                          if(code==200)
                          {
                              Message message=new Message();
                              message.what=UPDATE_SUCCESS;
                              handlerForUpdate.sendMessage(message);
                          }
                          else
                          {
                              Message message=new Message();
                              message.what=UPDATE_FAIL;
                              handlerForUpdate.sendMessage(message);
                          }
                          }catch (JSONException e){e.printStackTrace();}
                          catch (IOException e){e.printStackTrace();
                              Message message=new Message();
                              message.what=UPDATE_FAIL;
                              handlerForUpdate.sendMessage(message);}
                      }
                  }.start();
          }
      });
    }

    private void packMessage() {
        try {
            JSONObject object=new JSONObject();
            object.put("userName",name);
            object.put("password",password);
            object.put("institute",institute);
            object.put("identity",identity);
            object.put("uid",uid);
            toServer=String.valueOf(object);
        }catch (JSONException e){e.printStackTrace();}
    }

    private void collectMessage() {
        name=nameField.getText().toString().trim();
        password=passwordField.getText().toString().trim();
        institute=instituteField.getText().toString().trim();
        identity=identityField.getText().toString().trim();
    }

    private void setCancel() {
         cancel.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 finish();
             }
         });
    }

    private void initPage() {
        nameField=findViewById(R.id._update_user_username);
        passwordField=findViewById(R.id._update_user_password);
        instituteField=findViewById(R.id._update_user_institute);
        identityField=findViewById(R.id._update_user_identity);

        Intent intent=getIntent();
        name=intent.getExtras().getString("userName");
        password=intent.getExtras().getString("password");
        institute=intent.getExtras().getString("institute");
        identity=intent.getExtras().getString("identity");
        uid=intent.getExtras().getLong("uid");

        nameField.setText(name);
        passwordField.setText(password);
        instituteField.setText(institute);
        identityField.setText(identity);

        cancel=findViewById(R.id._update_user_cancel);
        confirm=findViewById(R.id._update_user_confirm);
    }
}